package com.yzjs.localhost.service.hrmUser.impl;

import com.alibaba.fastjson.JSON;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dao.hrmUser.HrmUserMapper;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmUser.HrmUserEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmUser.HrmUserService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("hrmUserService")
public class HrmUserServiceImpl extends BaseServiceImpl<HrmUserEntity, HrmUserMapper > implements HrmUserService {


    @Override
    public ComparisonResultDTO HrmUserCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce = dto.getBeforce();
        String beforceInfo = beforce.getInfo();
        Map<String, Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();

        ComparisonResultDTO resultDTO = null;
        if (beforce.getOperatorCode().contains("add")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 用户ID,部门ID，名称，性别，证件号码，电话，户籍地址
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "staffUserId,staffDeptId,fullName,user_sex,credentials_num,tel_phone,permanent_address");
        } else if (beforce.getOperatorCode().contains("edit")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 用户ID,部门ID，名称，性别，证件号码，电话，户籍地址
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "staffUserId,staffDeptId,fullName,user_sex,credentials_num,tel_phone,permanent_address");
        } else if (beforce.getOperatorCode().contains("remove")) {
            beforceParse.put("deleteFlag", "2");
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 用户状态
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "staffUserIds,deleteFlag");
            Object kycgId = afterParse.get("staffUserIds");
            if (kycgId == null) {
                resultDTO.setAbstractName("主键ID不存在");
                resultDTO.setWarningInfo("主键ID不存在");
                resultDTO.setMathFlag(false);
                return  resultDTO;
            }
            HrmUserEntity hrmUserEntity = new HrmUserEntity();
            hrmUserEntity.setStaffUserId(Long.valueOf((String) afterParse.get("staffUserIds")));
            hrmUserEntity.setDeleteFlag((String) afterParse.get("deleteFlag"));
            hrmUserEntity.setDataIdentification(after.getDataIdentification());
            this.insert(hrmUserEntity);
            return resultDTO;
        }

        // 记录表表信息
        HrmUserEntity hrmUserEntity = JsonUtil.json2pojo(after.getInfo(), HrmUserEntity.class);
        hrmUserEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmUserEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmUserEntity);

        return resultDTO;
    }
}